package miaofusdk

import (
	"encoding/json"
)

/**
授权中心的数据..
这里不做 json 的大小写转换了..
授权中心是第一个独立出来的服务..当时没考虑到 sdk 的问题..
授权中心涉及了好几个服务..暂时没时间一个个修改了.
*/
type AliAuthInfoBean struct {
	Id                 int
	Appid              string
	AppPid             string
	AppName            string
	Scope              string
	AccessToken        string
	UserId             string
	AuthAppId          string
	ExpiresIn          int64
	ReExpiresIn        int64
	RefreshToken       string
	Avatar             string
	NickName           string
	Province           string
	City               string
	Gender             string
	UserTypeValue      string
	UserStatus         string
	FirmName           string
	RealName           string
	Email              string
	CertNo             string
	Phone              string
	Mobile             string
	IsCertified        string
	IsBankAuth         string
	IsIdAuth           string
	IsMobileAuth       string
	IsLicenceAuth      string
	IsStudentCertified string
	IsCertifyGradeA    string
	CertTypeValue      string
	DeliverPhone       string
	DeliverMobile      string
	DeliverFullname    string
	Area               string
	Address            string
	Zip                string
	AuthType           string
}

type RespAliAuthInfo struct {
	Response
	Data AliAuthInfoBean `json:"data,omitempty"`
}

func (s *SrvAliAuthManager) GetAliAppAuthInfo(app_id, user_id, scope string) (RespAliAuthInfo, error) {
	resp := RespAliAuthInfo{}
	http_request := s.GetPost(s.GetUrl() + "/auth/alipay/userinfo")
	http_request.Param("app_id", app_id)
	http_request.Param("user_id", user_id)
	http_request.Param("scope", scope)
	if v, err := http_request.Bytes(); err != nil {
		return resp, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp, err
	} else {
		return resp, nil
	}
}

/**
授权中心的数据..
这里不做 json 的大小写转换了..
授权中心是第一个独立出来的服务..当时没考虑到 sdk 的问题..
授权中心涉及了好几个服务..暂时没时间一个个修改了.
*/
type WxAuthInfoBean struct {
	Id           int
	Appid        string
	AccessToken  string
	OpenId       string
	ExpiresIn    int64
	ReExpiresIn  int64
	RefreshToken string
	Scope        string
	NickName     string
	Sex          string
	Province     string
	City         string
	Country      string
	HeadimgUrl   string
	Privilege    string
	UnoinId      string
	AuthType     string
	JsapiTicket  string
}

type RespWxAuthInfo struct {
	Response
	Data WxAuthInfoBean `json:"data,omitempty"`
}

func (s *SrvAliAuthManager) GetWxAppAuthInfo(app_id, open_id, scope string) (RespWxAuthInfo, error) {
	resp := RespWxAuthInfo{}
	http_request := s.GetPost(s.GetUrl() + "/auth/wechat/userinfo")
	http_request.Param("app_id", app_id)
	http_request.Param("open_id", open_id)
	http_request.Param("scope", scope)
	if v, err := http_request.Bytes(); err != nil {
		return resp, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp, err
	} else {
		return resp, nil
	}
}

func (s *SrvAliAuthManager) GetWxAppMpAuth(app_id string) (RespWxAuthInfo, error) {
	resp := RespWxAuthInfo{}
	http_request := s.GetPost(s.GetUrl() + "/auth/wechat/get_mp_token")
	http_request.Param("app_id", app_id)
	if v, err := http_request.Bytes(); err != nil {
		return resp, err
	} else if err := json.Unmarshal(v, &resp); err != nil {
		return resp, err
	} else {
		return resp, nil
	}
}
